import Mock from 'mockjs'

/**
 * @date: 2024-10-10 17:45:16
 * @description: 随机生成2020年之后的时间
 * @modifyContent:
 * @author: Forever丿顾北
**/
const generateFutureDate = (format = 'yyyy-MM-dd HH:mm:ss') => {
  const start = new Date(2020, 0, 1) // 2020年1月1日
  const now = new Date()
  const min = start.getTime()
  const max = now.getTime()

  // 生成一个介于2020年1月1日与现在的随机时间戳
  const randomTimestamp = Math.floor(Math.random() * (max - min + 1)) + min
  const randomDate = new Date(randomTimestamp)
  // 提取各个部分
  const year = randomDate.getFullYear()
  const month = String(randomDate.getMonth() + 1).padStart(2, '0')
  const day = String(randomDate.getDate()).padStart(2, '0')
  const hours = String(randomDate.getHours()).padStart(2, '0')
  const minutes = String(randomDate.getMinutes()).padStart(2, '0')
  const seconds = String(randomDate.getSeconds()).padStart(2, '0')

  // 定义替换规则
  const replacements = {
    'yyyy': year,
    'yy': year.toString().slice(-2),
    'MM': month,
    'M': month[0],
    'dd': day,
    'd': day[0],
    'HH': hours,
    'H': hours[0],
    'hh': hours > 12 ? hours % 12 : hours,
    'h': hours > 12 ? hours % 12 : hours,
    'mm': minutes,
    'm': minutes[0],
    'ss': seconds,
    's': seconds[0]
  }

  // 替换格式化字符串
  let formattedDate = format
  for (const [pattern, value] of Object.entries(replacements)) {
    formattedDate = formattedDate.replace(new RegExp(pattern, 'g'), value)
  }

  // 返回格式化的日期字符串
  return formattedDate
}

/**
 * @date: 2024-10-10 10:54:51
 * @description: 获取菜单
 * @modifyContent:
 * @author: Forever丿顾北
**/
const getMenu = (config) => {
  const { useName, passWord } = JSON.parse(config.body)
  if (useName === 'admin' && passWord === '123456') {
    return {
      code: 200,
      msg: '登录成功',
      data: {
        menu: [
          {
            path: '/home',
            name: 'Home',
            label: '首页',
            icon: 'el-icon-s-home',
            url: 'home/index'
          },
          {
            path: '/commodity',
            name: 'Commodity',
            label: '商品管理',
            icon: 'el-icon-s-shop',
            url: 'commodity/index'
          },
          {
            path: '/user',
            name: 'User',
            label: '用户管理',
            icon: 'el-icon-s-custom',
            url: 'user/index'
          },
          {
            path: '/other',
            name: 'Other',
            label: '其他',
            icon: 'el-icon-s-grid',
            children: [
              {
                path: '/page_one',
                name: 'PageOne',
                label: '页面一',
                icon: 'el-icon-s-order',
                url: 'other/page_one'
              },
              {
                path: '/page_two',
                name: 'PageTwo',
                label: '页面二',
                icon: 'el-icon-s-order',
                url: 'other/page_two'
              }
            ]
          }
        ],
        token: Mock.Random.guid(),
        userInfo: {
          userName: 'Admin',
          userId: Mock.Random.guid(),
          userAvatar: Mock.Random.image('50x50', Mock.Random.color()),
          lastLoginTime: generateFutureDate('yyyy-MM-dd HH:mm:ss'),
          lastLoginAddress: Mock.mock('@county(true)'),
          userAuthority: '1'
        }
      }
    }
  } else if (useName === 'zhangsan' && passWord === 'zhangsan') {
    return {
      code: 200,
      msg: '登录成功',
      data: {
        menu: [
          {
            path: '/home',
            name: 'Home',
            label: '首页',
            icon: 'HomeOutlined',
            url: 'home/index'
          },
          {
            path: '/commodity',
            name: 'Commodity',
            label: '商品管理',
            icon: 'ShoppingOutlined',
            url: 'commodity/index'
          }
        ],
        token: Mock.Random.guid(),
        userInfo: {
          userName: '张三',
          userId: Mock.Random.guid(),
          userAvatar: Mock.Random.image('50x50', Mock.Random.color()),
          lastLoginTime: generateFutureDate('yyyy-MM-dd HH:mm:ss'), // 修正这里
          lastLoginAddress: Mock.mock('@county(true)'),
          userAuthority: '2'
        }
      }
    }
  } else {
    return {
      code: 500,
      msg: '账号或密码错误'
    }
  }
}

const Login = {
  getMenu // 获取菜单
}

export default Login
